<?php
class MallAction extends BaseAction
{
	public function _initialize()
	{
		//判断用户登录如果登录判断是否关注当前商场
		$this->assign('isMall', true);
		$this->assign('nav', 2);
		parent::_initialize();
	}

	public function index()
	{
	}

	/**
	 * 商场首页
	 */
	public function home($mall)
	{
		$mid = $mall['id'];
		//获取商场基本信息
		$mall = $this->getBaseInfo($mid);
		$FloorModel = D('Floor');
		$place = $FloorModel->getMallPlace($mid); // 分座
		//获取楼层信息
		$floors = $FloorModel->getList($mid);
		//判断是否有分座数据
		$DiscountModel = D('Discount');
		//分座 楼层 信息
		//获取楼层折扣信息
		$dis = $DiscountModel->getMallDis($mid);
		$ads = $this->getFloorAd($floors, $mall);
		$this->getMallOther($mid);
		//设置商场seo
		$this->mallSeoSet($mall['name']);
        $this->assign('day_time', $this->getThisDayTime());
		$this->assign('place', $place);
		$this->assign('mall', $mall);
		$this->assign('floors', $floors);
		$this->assign('floorAds', $ads);
		$this->assign('dis', $dis);
		$this->display('Mall:home');
	}

	/**
	 * 商场列表
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function mlist()
	{
		$this->seoSet('商场列表');
		$this->assign('nav', 2);
		$bus = $this->getCityBus();
		$this->assign('bus', $bus);
		$this->assign('isMall', false);
		$this->display();
	}

	/**
	 * 关于商场
	 * @param $mall
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function about($mall)
	{
		$this->assign('nav', 4);
		$mid = $mall['id'];
		//获取商场基本信息
		$mall = $this->getBaseInfo($mid);
		//商场滚动图
		$this->getMallOther($mid);
		$this->seoSet('关于商场 - ' . $mall['name'], null, msubstr(hh($mall['intr']), 0, 200));
		$this->assign('mall', $mall);
		$this->display('Mall:about');
	}

	/**
	 * 获取商场楼层品牌资讯
	 * @param $mall
	 */
	public function floor($mall, $floor)
	{
		$this->assign('nav', 3);
		$mid = $mall['id'];
		//获取商场基本信息
		$mall = $this->getBaseInfo($mid);
		$this->getMallOther($mid);
		$brandNews = D('BrandNews')->getBrandConsult($mid, $floor['id']);
		//设置商场seo
		$titles = array($mall['name'], $floor['name'], $floor['sain']);
		$this->seoSet($titles, $titles, msubstr(hh($mall['intr']), 0, 200));
		$this->assign('mall', $mall);
		$this->assign('brands', $brandNews);
		$this->assign('floor', $floor);
		$this->display('Mall:floor');
	}

	/**
	 * 获取商场楼层品牌资讯
	 * @param $mall
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function floors($mall)
	{
		$this->assign('nav', 3);
		$mid = $mall['id'];
		//获取商场基本信息
		$mall = $this->getBaseInfo($mid);
		$FloorModel = D('Floor');
		$place = $FloorModel->getMallPlace($mid); // 分座
		//获取楼层信息
		$floors = $FloorModel->getList($mid);
		//根据楼层信息获取楼层资讯
		if ($floors) {
			$floors = $this->setMallConsult($mid, $floors);
		}
		$titles = array($mall['name'], '楼层导航');
		$this->seoSet($titles, $titles, msubstr(hh($mall['intr']), 0, 200));
		$this->getMallOther($mid);
		$this->assign('place', $place);
		$this->assign('floors', $floors);
		$this->assign('mall', $mall);
		$this->display('Mall:floors');
	}

	/**
	 * 商场特卖
	 * @param $mall
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function discount($mall)
	{
		$this->assign('nav', 5);
		$dis = D('MallDiscount')->getCityMallDis(null, $mall['id']);
		$titles = array($mall['name'], '特卖汇总');
		$this->seoSet($titles, $titles, msubstr(hh($mall['intr']), 0, 200));
		$this->assign('dis', $dis);
		$this->assign('mall', $mall);
		$this->display('Mall:discount');
	}

	/**
	 * 设置商场对应楼层广告信息
	 * @param Int $id 楼层ID
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	protected function getFloorAd($floors, $mall)
	{
		$BrandNewsModel = D('BrandNews');
		foreach ($floors as $key => $floor) {
			$cacheName = 'mall-floor-ad-' . $floor['mid'] . '-' . $floor['id'];
			$ads = $this->getCache($cacheName);
			if ($ads) {
				$floors[$key] = $ads;
			} else {
				$ads = $BrandNewsModel->getListAd(17, $floor['mid'], $floor['id']);
				if (count($ads) > 8) {
					$allCount = count($ads);
					$BrandModel = D('Brand');
					for ($i = 8; $i < $allCount; $i++) {
						$ads[$i]['brand'] = $BrandModel->getOneLine($ads[$i]['bid']);
					}
				}
				$floors[$key]['ads'] = $ads;
				$floors[$key]['image_url'] = strtolower(UPLOAD_URL . 'malls/' . $mall['initial'] . '/' . $floor['sain'] . '.png');
				$floors[$key]['floor_url'] = strtolower(U('/' . $mall['initial'] . '/' . $floor['sain']));
				$this->setCache($cacheName, $floors[$key]);
			}
		}
		return $floors;
	}

	/**
	 * 获取商场基本信息
	 * @param $mid
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	protected function getBaseInfo($mid)
	{
		$cacheName = 'mall-baseinfo-' . $mid;
		//商场基本信息
		$mall = $this->getCache($cacheName);
		if (empty($mall)) {
			$MarketModel = D('Market');
			$mall = $MarketModel->find($mid);
			if (!$mall) {
				$this->errorLang('_MALL_NAME_ERROR_');
			}
			$mall['image_url'] = $MarketModel->getMarketImage($mall['image']);
			$this->setCache($cacheName, $mall);
		}
		//判断是否登陆
		$uid = getLoginUserInfo();
		if ($uid) {
			//判断是否关注
			D('MarketAttention')->checkUserIsAttrMall($uid, $mall);
		}
		//商场滚动图
		$slider = $this->getSlide($mid);
		$this->assign('slider', $slider);
		return $mall;
	}

	/**
	 * 获取商场关注、品牌、资讯数
	 * @param $mid
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	protected function getMallOther($mid)
	{
		//关注度 品牌数 TODO 折扣信息数量 AJAX延迟获取、分座数据获取
		$attrCount = D('MarketAttention')->findAttCount($mid);
		$brandCount = D('FloorBrand')->getMallBrandCount($mid);
		$brandNewCount = D('BrandNews')->getMallNewCount($mid);
		$dis = D('Discount')->getMallDis($mid);//商场资讯
		D('Market')->attCheckCount($mid, $attrCount);
		$this->assign('dis', $dis);
		$this->assign('attrCount', $attrCount);
		$this->assign('brandCount', $brandCount);
		$this->assign('brandNewCount', $brandNewCount);
	}

	/**
	 * 设置商场全部楼层品牌资讯
	 * @param Int $mid 商场标识
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	private function setMallConsult($mid, $mallFloors)
	{
		$floors = $this->getCache('set-market-consult-' . $mid);
		if (empty($floors)) {
			$BrandNewsModel = D('BrandNews');
			$map = array();
			//获取楼层品牌打折信息
			foreach ($mallFloors as $k => $floor) {
				$mallFloors[$k]['dis'] = $BrandNewsModel->getBrandConsult($mid, $floor['id']);
			}
			$this->setCache('setMarketConsult-' . $mid, $mallFloors);
			return $mallFloors;
		}
		return $floors;
	}
}

?>